import java.net.*;
import java.util.*;

abstract class AbstractHandler implements Handler {

  private String name;

  protected final String OK = "OK";
  protected final String NO = "NO";

  AbstractHandler(String name) {
    this.name = name;
  }

  /**
   * Uses the name <em>Name</em> if this is called
   * <em>Name</em><code>Handler</code>.  Othewise uses the simple
   * class name.
   */
  AbstractHandler() {
    this.name = handlerName(getClass());
  }

  private static String handlerName(Class cls) {
    String name = cls.getSimpleName();
    int idollar = name.indexOf("$");
    if (idollar != -1) {
      name = name.substring(idollar+1);
    }
    int ihandler = name.indexOf("Handler");
    return ihandler == -1 ? name : name.substring(0,ihandler);
  }

  protected final String get(Map<String,String> args, String name) {
    String v = args.get(name);
    try {
      return URLDecoder.decode(v);
    } catch (Exception e) {}
    return v;
  }

  public final String name() {
    return name;
  }
}